package com.zhou.org.controller;

import com.zhou.framework.util.PageUtil;
import com.zhou.framework.util.SettingUtil;
import com.zhou.util.AESUtil;
import com.zhou.util.ObjectUtil;
import com.zhou.util.StringTool;
import com.zhou.framework.aop.anotation.LogOption;
import com.zhou.framework.aop.enums.LogLevel;
import com.zhou.framework.config.PasswordPolicyConfig;
import com.zhou.framework.config.SystemConfig;
import com.zhou.framework.jwt.annotation.PassToken;
import com.zhou.framework.model.RS;
import com.zhou.org.dao.Setting;
import com.zhou.org.dto.SettingQueryDto;
import com.zhou.org.dto.SettingSaveDto;
import com.zhou.org.service.SettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lang.zhou
 * @since 2022-08-23
 */
@Api(tags = "系统参数接口")
@RestController
@RequestMapping("/sys/setting")
public class SettingController {
    @Autowired
    private SettingService settingService;

    @ApiOperation("参数模块列表")
    @LogOption(LogLevel.QUERY)
    @GetMapping("/getModule")
    public RS getModule(){
        return RS.data(settingService.getSettingModule());
    }

    @LogOption(LogLevel.QUERY)
    @ApiOperation("参数列表")
    @PostMapping("/list")
    public Object getPageList(@RequestBody SettingQueryDto dto){
        PageUtil.offsetPage(dto);
        List<Setting> list = settingService.list(dto);
        return PageUtil.result(dto,list);
    }


    @LogOption(value = LogLevel.SYSTEM,response = true)
    @ApiOperation("修改参数")
    @PostMapping("/save")
    public RS save(@RequestBody @Validated SettingSaveDto dto){
        Setting setting = new Setting();
        BeanUtils.copyProperties(dto,setting);

        if(Objects.equals(dto.getType(),0)){
            Setting s = settingService.getSetting(dto.getModule(), dto.getName());
            if(s != null){
                return RS.failed("参数已存在");
            }
        }
        settingService.saveSetting(setting);
        return RS.success();
    }

    @LogOption(value = LogLevel.SYSTEM,response = true)
    @ApiOperation("修改密码安全策略")
    @PostMapping("/savePasswordPolicy")
    public RS savePasswordPolicy(@RequestBody Map<String,Object> map){
        settingService.updateValue("password-policy",map);
        PasswordPolicyConfig.refresh();
        SettingUtil.refreshCache();
        return RS.success();
    }
    @LogOption(value = LogLevel.SYSTEM,response = true)
    @ApiOperation("修改系统安全策略")
    @PostMapping("/saveSystemPolicy")
    public RS saveSystemPolicy(@RequestBody Map<String,Object> map){
        List<String> allowSet = Arrays.asList("refererCheck","xFrameOptions","location","crossOrigin");
        map.keySet().removeIf(s -> !allowSet.contains(s));
        settingService.updateValue("system",map);
        SystemConfig.refresh();
        return RS.success();
    }

    @LogOption(value = LogLevel.OFF)
    @ApiOperation("模块名参数名获取参数")
    @GetMapping("/get")
    @PassToken
    public RS get(@RequestParam("module") String model,@RequestParam("name") String name){
        String s = SettingUtil.getString(model, name);
        return RS.data(AESUtil.encrypt(s));
    }

    @LogOption(value = LogLevel.OFF)
    @ApiOperation("参数map")
    @GetMapping("/map")
    public Object map(@RequestParam String module){
        Map<String, Object> settingMap = SettingUtil.getSettingMap(module);
        if(ObjectUtil.isNotEmpty(settingMap)){
            settingMap.remove("pri_key");
            for (Map.Entry<String, Object> entry : settingMap.entrySet()) {

                entry.setValue(AESUtil.encrypt(StringTool.safeToString(entry.getValue(),"")));
            }
        }
        return RS.data(settingMap);
    }
}

